import * as patrolService from '../../services/patrol';
import {queryAssetType} from '../../services/assetType';
import {qryAllUser} from '../../services/user';
import {queryAllRoomList} from '../../services/tRoom';

export default {
  namespace: 'patrol',
  state: {
    list: [],  
    roomList:[], 
    userList:[],  
    rackList:[],
    current : 1,
    currentSize : 10,
    total : 0, 
    modalVisible : false,
    modalType : 'create',
    newKey : '', 
    currentItem: {}, 
    patrolItem:{},
    modalViewVisible:false,
    detailList:[],
    assetCurrent:1,
    assetCurrentSize : 10,
    assetTotal : 0, 

  },
  reducers: {
    queryUserSuc(state, { payload:userList}) {
      return { ...state, userList };
    },
    queryListSuc(state, { payload }) {
      return { ...state, ...payload };
    },        
    queryRoomListSuc(state, { payload: roomList }) {
      return { ...state, roomList };
    },    
  
    showModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , modalVisible: true }
    },
    hideModal (state) {
      return { ...state, modalVisible: false, newKey : new Date().getTime() + '' }
    },
    showViewModal (state, { payload }) {
        return { ...state, ...payload, newKey : new Date().getTime() + '' , modalViewVisible: true }
    },
    hideViewModal (state) {
      return { ...state, modalViewVisible: false, newKey : new Date().getTime() + '' }
    },   
    qryPatrolByIdSuc(state, { payload }) {
      return { ...state, ...payload };
    }, 
    getPatrolListSuc(state, { payload }) {
      return { ...state, ...payload };
    }, 
    
  },
  effects: {    
    *queryUser({ payload },{ call, put }) {    
      const { body }  = yield call(qryAllUser);
      yield put({ type: 'queryUserSuc', payload: body.data });
    },
    *queryAllRoomList({ payload },{ call, put }) {
      const { body } = yield call(queryAllRoomList,payload);
      yield put({ type: 'queryRoomListSuc', payload: body.data });
    }, 
    *queryList({ payload },{ call, put }) {
      const { body } = yield call(patrolService.queryPatrolList,payload);
      yield put({ type: 'queryListSuc', payload: { list:body.data, current:body.current, currentSize:body.size, total:body.iTotalRecords}});
    },
    *qryRackList({ payload },{ call, put }) {     
      const { body }  = yield call(qryRackList,payload);
      yield put({ type: 'qryRackListSuc', payload:body.data});
    },    
    *create({ payload },{ call, put }){
      const { body } = yield call(patrolService.createPatrol,payload);
       yield put({ type: 'hideModal'});
       yield put({ type: 'reload' ,payload:{}});
    },
    *qryById({ payload },{ call, put }){
      const { body } = yield call(patrolService.qryPatrolById,payload);
       yield put({
          type: 'showModal',
          payload: {
            currentItem: body.data,
            modalType: 'edit',
            title: '编辑盘点',
          },
      });
    },  
    //查看巡检计划下的巡检单结果
    *getPatrolResult({ payload },{ call, put }) {
      yield put({ type: 'showViewModal'});
      const { body } = yield call(patrolService.qryPatrolById,payload.patrolId);
      yield put({ 
        type: 'qryPatrolByIdSuc', 
        payload: { 
            patrolItem:body.data,
          },
      });
      yield put({ type: 'getPatrolResultList',payload:payload});      
    },
    //查看巡检计划下的巡检单
    *getPatrolResultList({ payload },{ call, put }) {
      const { body } = yield call(patrolService.getPatrolResultList,payload);
      yield put({ type: 'getPatrolListSuc', payload: { detailList:body.data,patrolId:payload.patrolId, assetCurrent:body.current, assetCurrentSize:body.size, assetTotal:body.iTotalRecords}});
    },
    *edit({ payload },{ call, put }){
      const { body } = yield call(patrolService.editPatrol,payload);
      yield put({ type: 'hideModal'});
      yield put({ type: 'reload' ,payload:{}});
    }, 
    *remove({ payload },{ call, put }){
      const { body } = yield call(patrolService.removePatrol,payload.id);
      yield put({ type: 'reload' ,payload:payload.search});
    },  
    *reload({ payload },{ call, put }){
       yield put({ type: 'queryList',payload});
    },      
   
  },
  subscriptions: {
    setup({ dispatch, history }) {
      return history.listen(({ pathname }) => {
        if (pathname === '/patrol') {
          dispatch({ type: 'queryList', payload:{ 'name':'' }});
          dispatch({ type: 'queryUser' });
          dispatch({ type: 'queryAllRoomList' });          
         
        }
      });
    },
  },
};
